package com.pb.camera.work;

import android.net.wifi.ScanResult;

import com.pb.camera.constants.MyContants;
import com.pb.camera.utils.Assert;
import com.tutk.IOTC.AVAPIs;
import com.tutk.IOTC.AVIOCTRLDEFs;

/*
 * Creared by liufei on 2016 03/16
 * 控制类，用来从手机端向摄像头端发送命令使用
 */
public class ControlTask {
    private final static int CTRL_BUFF_SIZE_1 = 8;
    private final static int CTRL_BUFF_SIZE_2 = 64;
    private final static int CTRL_BUFF_SIZE_3 = 128;
    private final static int CTRL_BUFF_SIZE_4 = 1024;
    private static AVAPIs mDefault;

    private static AVAPIs getDefault() {
        if (mDefault == null)
            mDefault = new AVAPIs();
        return mDefault;
    }

    public static boolean getDeviceInfo(int avIndex) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlDeviceInfoReq.parseContent();
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_DEVINFO_REQ, bytes, bytes.length);
        return ac < 0 ? false : true;
    }

    public static boolean getVideoMode(int avIndex) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlGetVideoModeReq.parseContent(0);
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_GET_VIDEOMODE_REQ, bytes, CTRL_BUFF_SIZE_1);
        return ac < 0 ? false : true;
    }

    public static boolean getMotionDetect(int avIndex) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlGetMotionDetectReq.parseContent(0);
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_GETMOTIONDETECT_REQ, bytes, CTRL_BUFF_SIZE_1);
        return ac < 0 ? false : true;
    }

    public static boolean setVideoMode(int avIndex, byte mode) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlSetVideoModeReq.parseContent(avIndex, mode);
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SET_VIDEOMODE_REQ, bytes, CTRL_BUFF_SIZE_1);
        return ac < 0 ? false : true;
    }

    public static boolean setMotionDetect(int avIndex, int sensitivity) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlSetMotionDetectReq.parseContent(0, sensitivity);
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SETMOTIONDETECT_REQ, bytes, CTRL_BUFF_SIZE_1);
        return ac < 0 ? false : true;
    }

    public static boolean setRecordMode(int avIndex, int recordType) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlSetRecordReq.parseContent(avIndex, recordType);
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SETRECORD_REQ, bytes, CTRL_BUFF_SIZE_2);
        return ac < 0 ? false : true;
    }

    public static boolean setNightEnviroment(int avIndex, byte modle) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlSetEnvironmentReq.parseContent(0, modle);
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SET_ENVIRONMENT_REQ, bytes, CTRL_BUFF_SIZE_2);
        return ac < 0 ? false : true;
    }

    public static boolean getEnviroment(int avIndex) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlGetEnvironmentReq.parseContent(0);
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_GET_ENVIRONMENT_REQ, bytes, CTRL_BUFF_SIZE_2);
        return ac < 0 ? false : true;
    }
    
    public static boolean getResolution(int avIndex) {
    	AVAPIs avAPIS = getDefault();
    	byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlGetStreamCtrlReq.parseContent(0);
    	int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_GETSTREAMCTRL_REQ, bytes, CTRL_BUFF_SIZE_2);
    	return ac < 0 ? false : true;
    }

    public static boolean getRecordMode(int avIndex) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlGetRecordReq.parseContent(avIndex);
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_GETRECORD_REQ, bytes, CTRL_BUFF_SIZE_2);
        return ac < 0 ? false : true;
    }

    public static boolean getListWifi(int avIndex) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlListWifiApReq.parseContent();
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_LISTWIFIAP_REQ, bytes, CTRL_BUFF_SIZE_1);
        return ac < 0 ? false : true;
    }

    public static boolean getWifi(int avIndex) {
        AVAPIs avAPIS = getDefault();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlGetWifiReq.parseContent();
        int ac = avAPIS.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_GETWIFI_REQ, bytes, CTRL_BUFF_SIZE_1);
        return ac < 0 ? false : true;
    }

    public static boolean setWifi(int avIndex, ScanResult wifiScan, String wifiPassword) {
        Assert.notNull(wifiScan);
        Assert.notNull(wifiPassword);
        AVAPIs avapIs = getDefault();
        byte[] ssid = wifiScan.SSID.getBytes();
        byte[] password = wifiPassword.getBytes();
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlSetWifiReq.parseContent(ssid, password, (byte) 1, (byte) getEnctype(wifiScan.capabilities));
        int ac = avapIs.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SETWIFI_REQ, bytes, CTRL_BUFF_SIZE_3);
        return ac < 0 ? false : true;
    }

    public static void checkUpdate(String model, RequestCallBack<String> requestCallBack) {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, MyContants.UPDATE_CAM_INFO_URL + model, requestCallBack);
    }

    private static byte getEnctype(String encrpte) {
        if (encrpte.contains("WEP"))
            return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_WEP;
        if (encrpte.contains("WPA-TKIP"))
            return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_WPA_TKIP;
        if (encrpte.contains("WPA-AES"))
            return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_WPA_AES;
        if (encrpte.contains("WPA2-TKIP"))
            return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_WPA2_TKIP;
        if (encrpte.contains("WPA2-AES"))
            return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_WPA2_AES;
        if (encrpte.contains("WPA-PSK-TKIP"))
            return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_WPA_PSK_TKIP;
        if (encrpte.contains("WPA-PSK-AES"))
            return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_WPA_PSK_AES;
        if (encrpte.contains("WPA2-PSK-TKIP"))
            return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_WPA2_PSK_TKIP;
        if (encrpte.contains("WPA2-PSK_AES") || encrpte.contains("PSK-CCMP"))
            return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_WPA2_PSK_AES;
        return AVIOCTRLDEFs.AVIOTC_WIFIAPENC_INVALID;
    }

    public static void checkUpdateStatus(int avIndex, String mModel, String mLastestVersion, String mUpdateUrl) {
        AVAPIs avapIs = getDefault();
        AVIOCTRLDEFs.ACCESS_OTA_FILE_CONFIG_REQ_CLASS configReq = new AVIOCTRLDEFs.ACCESS_OTA_FILE_CONFIG_REQ_CLASS();
        byte[] bytes = AVIOCTRLDEFs.ACCESS_OTA_FILE_CONFIG_REQ_CLASS.parseContent(mLastestVersion, mModel, mUpdateUrl);
        int re = avapIs.avSendIOCtrl(avIndex, AVIOCTRLDEFs.ACCESS_OTA_FILE_CONFIG_REQ, bytes, bytes.length + 1);
    }

    public static void downLoadFile(int avIndex) {
        AVAPIs avapIs = getDefault();
        int re = avapIs.avSendIOCtrl(avIndex, AVIOCTRLDEFs.ACCESS_OTA_FILE_DOWNLOAD_REQ, null, 0);
    }

    public static void checkDownloadStatus(int avIndex) {
        AVAPIs avapIs = getDefault();
        int re = avapIs.avSendIOCtrl(avIndex, AVIOCTRLDEFs.ACCESS_OTA_FILE_OK_CHECK_REQ, null, 0);
    }

    public static void processUpdate(int avIndex) {
        AVAPIs avapIs = getDefault();
        int re = avapIs.avSendIOCtrl(avIndex, AVIOCTRLDEFs.ACCESS_OTA_DEVICE_UPDATE_REQ, null, 0);
    }

    public static void checkUpdateStatus(int avIndex) {
        AVAPIs avapIs = getDefault();
        int re = avapIs.avSendIOCtrl(avIndex, AVIOCTRLDEFs.ACCESS_OTA_DEVICE_UPDATE_CHECK_REQ, null, 0);
    }

    public static void GetVideoSet(int avIndex) {
        AVAPIs avapIs = getDefault();
        int re = avapIs.avSendIOCtrl(avIndex, AVIOCTRLDEFs.ACCESS_GET_CONFIG, null, 0);
    }

    public static void upLoadAvSet(int avIndex,String  name , int value) {
        AVAPIs avapIs = getDefault();
        byte[] dummy_ll_compa = { 0x06, 0x01, 0x7f, 0x1f, 0x04,
                0x00, 0x00, 0x00, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
                0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f };
        String string_ll_compa = new String(dummy_ll_compa);
        String load_ll_compa = name+value+";";
        StringBuffer sb_ll_compa = new StringBuffer(string_ll_compa);
        sb_ll_compa.append(load_ll_compa);

        byte[] packetGetParam_ll_compa = sb_ll_compa.toString()
                .getBytes();
        dummy_ll_compa[4] = (byte)packetGetParam_ll_compa.length;
        int re = avapIs.avSendIOCtrl(0, AVIOCTRLDEFs.ACCESS_AV_CONFIG,
                packetGetParam_ll_compa,packetGetParam_ll_compa.length+1);
    }

    public static boolean setEnviroment(int avIndex, byte avioctrlEnvironmentNight) {
        AVAPIs avapIs = getDefault();
        byte[] setInfo = AVIOCTRLDEFs.SMsgAVIoctrlSetEnvironmentReq.parseContent(avIndex, avioctrlEnvironmentNight);
        int re = avapIs.avSendIOCtrl(avIndex, AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SET_ENVIRONMENT_REQ,setInfo , CTRL_BUFF_SIZE_1);
        return re < 0 ? false : true;
    }

    public  static  boolean setResolutionAction(int avIndex,byte quality){
        AVAPIs avapIs = getDefault();
        byte[] bytes =  AVIOCTRLDEFs.SMsgAVIoctrlSetStreamCtrlReq.parseContent(0, quality);
        int result = avapIs.avSendIOCtrl(avIndex,AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SETSTREAMCTRL_REQ ,bytes,bytes.length+1);
        return result < 0 ? false : true;
    }

    public static boolean getListEvent(int avIndex,long startTime,long endTime,byte event,byte status){
        AVAPIs avapIs = getDefault();
        byte[] bytes =  AVIOCTRLDEFs.SMsgAVIoctrlListEventReq.parseConent(avIndex, startTime, endTime, event, status);
        int result = avapIs.avSendIOCtrl(avIndex,AVIOCTRLDEFs.IOTYPE_USER_IPCAM_LISTEVENT_REQ,bytes,bytes.length+1);
        return result < 0 ? false : true;
    }

    public static boolean playRecord(int avIndex , int command , int param,AVIOCTRLDEFs.SAvEvent event) {
        AVAPIs avapIs = getDefault();
        byte[] stTimeDay = AVIOCTRLDEFs.STimeDay.parseContent(event.utctime);
        byte[] bytes = AVIOCTRLDEFs.SMsgAVIoctrlPlayRecord.parseContent(avIndex,command,param,stTimeDay);
        int result = avapIs.avSendIOCtrl(avIndex,AVIOCTRLDEFs.IOTYPE_USER_IPCAM_RECORD_PLAYCONTROL,bytes,bytes.length+1);
        return result < 0 ? false : true;
    }
}

